<?php
class indexController extends siteController
{
    public function info() {
       $keyword=urldecode($_GET['keyword']);
        if(!is_utf8($keyword))
        {
            $keyword=auto_charset($keyword,'gbk','utf-8');
        }
        $keyword = msubstr(in($keyword),0,10);
        $keywords = preg_replace ('/\s+/',' ',$keyword); 
        $keywords=explode(" ",$keywords);
        if(empty($keywords[0])){
            $this->alert('没有关键词！');
        }

        //导航
        $this->nav=array(
            0=>array('name'=>$keyword.' 搜索','curl'=>url('index/info',array('model'=>$model,'keyword'=>urlencode($keyword)))),
        );

        //基本信息
        $this->info=array(
            'name'=>$keyword,
            'model'=>$model,
            );
        $this->keyword=$keyword;

        //MEDIA信息
        $this->common=api('default','common_media',array('title'=>$keyword.' 搜索','keywords'=>$keyword,'description'=>$keyword));

        //获取栏目
        $cid= intval($_GET['cid']);
        if($cid){
            $cid= api('default','category_getcat',$cid);
            $where_cid=' B.cid in('.$cid.') AND ';
        }

        //获取搜索类型
        $model=intval($_GET['model']);
        if(empty($model)){
            $model=0;
        }
        
        $config=config('TPL');
        //设置分页
        $size = intval($config['TPL_TEMPLATE_SEARCH_PAGE']); 
        if (empty($size)) {
            $listrows = 10;
        } else {
            $listrows = $size;
        }
        $url=url('index/info',array('model'=>$model,'keyword'=>urlencode($keyword),'page'=>'{page}'));
        $limit=$this->pageLimit($url,$listrows);


        $module=in($_GET['module']);
        if(empty($module)){
            //处理搜索字段
            $where='A.status=1 AND '.$where_cid;
            foreach ($keywords as $value) {
                switch ($model) {
                    //标题+描述+关键词
                    case '1':
                        $where2.= ' ( A.title like "%' . $value . '%" or A.keywords like "%' . $value . '%" or A.description like "%' . $value . '%" )';
                        break;
                    //标题+描述+关键词+全文
                    case '2':
                        $where2.= ' ( A.title like "%' . $value . '%" or A.keywords like "%' . $value . '%" or A.description like "%' . $value . '%" or C.content like "%' . $value . '%"  )';
                        break;
                    //标题
                    default:
                        $where2.= ' ( A.title like "%' . $value . '%"  )';
                        break;
                }
            }
            //获取搜索列表
            $loop=model('search_list')->search_list($where.$where2,$limit,$model);
            //获取分页数
            $count=model('search_list')->search_count($where.$where2,$model);

        }else{
            //API搜索模块
            $loop=api($module,'search_list',array('limit'=>$limit,'model'=>$model,'keywprds'=>$keywords,'cid'=>$cid));
            $count=api($module,'search_count',array('limit'=>$limit,'model'=>$model,'keywprds'=>$keywords,'cid'=>$cid));
        }

        $this->loop=$loop;
        $this->conut=$count;

        //获取分页
        $this->page=$this->pageShow($count);

        if(!empty($module)){
            $tpl=api($module,'search_list_tpl');
            if($tpl){
                $this->show($tpl);
            }else{
                $this->show($config['TPL_TEMPLATE_SEARCH']);
            }
        }else{
            $this->show($config['TPL_TEMPLATE_SEARCH']);
        }
            

    }

}
